package com.fuja.fumo.handler;
import org.apache.log4j.Logger;

import com.fuja.fumo.model.Option;
import com.fuja.fumo.model.Question;
import com.fuja.swf.ui.UIUtil;

import superwaba.ext.xplat.xml.XmlTokenizer;
import waba.fx.Color;
import waba.sys.Convert;
import waba.ui.*;
import waba.util.Vector;
public class MockHandler extends XmlTokenizer {
	private static Logger log = Logger.getLogger(MockHandler.class);
	private String currTag="";
	private String currAttrib="";
	private Vector questions = new Vector();
	private Question question;
	private Option option;
	private ProgressBar bar = new ProgressBar(0,100);
	public MockHandler(){
		super();
		bar.textColor = Color.YELLOW;
		bar.setBackForeColors(Color.BLACK, Color.BLUE);
	}
	public void foundStartOfInput(byte buffer[], int offset, int count) {
		log.debug("Start");
		//debug("Start: " +  new String(buffer, offset, count));
	}

	public void foundStartTagName(byte buffer[], int offset, int count) {
		currTag = new String(buffer, offset, count);
		log.debug("StartTagName: " + currTag);
		
		if (currTag.equals("Option")){
			option = new Option();
		}else if (currTag.equals("Question")){
			question = new Question();
			bar.setValue(bar.getValue() + 1);
			UIUtil.getActual().repaintNow();
		}
	}
	public void foundEndTagName(byte buffer[], int offset, int count) {
		currTag = "";
		String tag = new String(buffer, offset, count);
		log.debug("EndTagName: " + tag);
		
		if (tag.equals("Question") && question!=null){
			questions.addElement(question);
		}else if (tag.equals("Option") && question!=null){
			question.getOptions().addElement(option);
		}
	}
	public void foundCharacterData(byte buffer[], int offset, int count) {
		String value = new String(buffer, offset, count).trim();
		log.debug("Content: " + value);
		if ((currTag.equals("Question-description")|| currTag.equals("Quest-desc")) && question!=null){
			question.setDescription(value);
		} else if (currTag.equals("Option-description") || currTag.equals("Opt-desc")){
			option.setDescription(value);
		} else if (currTag.equals("Reason")){
			option.setReason(value);
		}
	}
	public void foundAttributeName(byte buffer[], int offset, int count) {
		currAttrib = new String(buffer, offset, count).trim();
		log.debug("AttributeName: " + currAttrib);
		
		if (currAttrib.equals("picture-URL")){
			currTag = "";
			currAttrib = "";
			question = null;
		}
	}
	public void foundAttributeValue(byte buffer[], int offset, int count, byte dlm) {
		String value = new String(buffer, offset, count).trim();
		log.debug("AttributeValue: " + value);
		
		if (currTag.equals("Option") && currAttrib.equals("value") && value.toLowerCase().equals("true")){
			option.setValue(true);
		}else if (currTag.equals("Question")){
			if (currAttrib.equals("id")){
				question.setId(Convert.toInt(value));
			}else if (currAttrib.equals("objective")){
				question.setObjective(value);
			} 
		}else if (currTag.equals("Mock-test") && currAttrib.equals("number-of-questions")){
			bar.max = Convert.toInt(value);
			UIUtil.getActual().clear();
			UIUtil.getActual().add(bar,Window.CENTER,Window.CENTER);
		}
		currAttrib = "";
	}
	
	protected void foundEndOfInput(int count) {
		UIUtil.getActual().remove(bar);
	}
	public Vector getQuestions() {
		return questions;
	}
}
